package com.leetcode.contest;

import com.leetcode.common.ListNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/11/4 9:41
 */
public class LC2058找出临界点之间的最小和最大值 {

    public static int[] nodesBetweenCriticalPoints(ListNode head) {
        List<Integer> indices = new ArrayList<>();
        if (head.next == null || head.next.next == null) {
            //节点不足三个，直接返回
            return new int[]{-1, -1};
        }
        int[] answer = {Integer.MAX_VALUE, Integer.MIN_VALUE};
        ListNode prev = head, cur = head.next, next = head.next.next;
        int i = 2;
        while (next != null) {
            if ((cur.val > next.val && cur.val > prev.val) || (cur.val < next.val && cur.val < prev.val)) {
                indices.add(i);
            }
            i++;
            prev = prev.next;
            cur = cur.next;
            next = next.next;
        }
        if (indices.size() < 2) return new int[]{-1, -1};
        for (int j = 1; j < indices.size(); j++) {
            answer[0] = Math.min(answer[0], indices.get(j) - indices.get(j - 1));
        }
        answer[1] = indices.get(indices.size() - 1) - indices.get(0);
        System.out.println(indices);
        return answer;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(5, new ListNode(3, new ListNode(1, new ListNode(2,
                new ListNode(5, new ListNode(1, new ListNode(2)))))));
        System.out.println(Arrays.toString(nodesBetweenCriticalPoints(head)));
    }
}
